//
//  CourseMessageViewController.swift
//  ddx0513
//
//  Created by shuijing on 15/7/26.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class CourseMessageViewController: UITableViewController {
    var teacherIntroImgView: UIImageView! = UIImageView(frame: CGRectMake(10, 36, 63, 75))
    var lessonId:Int!
    var tearchImgUrl = ""
    var hud: MBProgressHUD?
    
    var content:NSMutableArray = []
    var courseIntroHeight: CGFloat = 0
    var teacherIntroHeight: CGFloat = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.tableView.tableFooterView = UIView(frame: CGRectZero)
        self.teacherIntroImgView.image = UIImage(named: "教师默认头像.png")
        
        var header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: Selector("getCourseMessage"))
        header.lastUpdatedTimeLabel.hidden = true
        self.tableView.header = header
        self.tableView.header.beginRefreshing()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func getCourseMessage(){
        var a: [String: AnyObject]?
        if (DBUtils.mem.loginStatus) {
            var userid:Int = DBUtils.mem.userId
            var token:String = DBUtils.mem.token
            a = ["userid":userid,"token":token,"lessonid":lessonId]
        } else {
            a = ["lessonid":lessonId]
        }
        
        NetUtils.netRequest(Method.GET, URLString: NetUtils.getURLLessonInfo() , parameters: a, responseHandler: nil, netErrorHandler: errorHandler, successHandler: courseMessage, interErrorHandler: interErrorHandler, jsonErrorHandler: errorHandler)
    }
    
    func errorHandler() {
        self.tableView.header.endRefreshing()
        MBHudUtils.showText("加载失败", view: self.view)
    }
    
    func interErrorHandler(retCode: Int, retMsg: String) {
        self.tableView.header.endRefreshing()
        MBHudUtils.showText("加载失败", view: self.view)
    }
    
    func courseMessage (json: JSON) {
        content.addObject(json["intro"].string!)
        content.addObject(json["teacher"]["intro"].string!)
        
        let tearchUrl = json["teacher"]["imgurl"].string
        
        if let a = tearchUrl {
            self.tearchImgUrl = a
            DBUtils.getImageFromFile(a, successHandler: { (imgUrl, img) -> Void in
                if (imgUrl == self.tearchImgUrl) {
                    self.teacherIntroImgView.image = img
                }
            })
        }
        
        self.courseIntroHeight = ViewUtils.heightForTextviewString(content[0] as! String, fontSize: 14, width: UIScreen.mainScreen().bounds.size.width - 20)
        self.teacherIntroHeight = ViewUtils.heightForTextviewString(content[1] as! String, fontSize: 14, width: UIScreen.mainScreen().bounds.size.width - 98)
        
        tableView.reloadData()
        self.tableView.header.endRefreshing()
        self.tableView.header.hidden = true
        
    }

    // MARK: - TableView Delegate
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return content.count
    }
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("CourseIntroCell") as! UITableViewCell
        
        if (indexPath.row == 0) {
            var label = UILabel(frame: CGRectMake(10, 10, 200, 21))
            let font = UIFont.boldSystemFontOfSize(15)
            label.font = font
            label.textColor = UIColor.blackColor()
            label.text = "课程简介"
            cell.addSubview(label)
            
            var courseIntroTextView = UITextView(frame: CGRectMake(10, 31, UIScreen.mainScreen().bounds.size.width - 20, courseIntroHeight))
            let font2 = UIFont.systemFontOfSize(14)
            courseIntroTextView.font = font2
            courseIntroTextView.textColor = UIColor.grayColor()
            courseIntroTextView.editable = false
            courseIntroTextView.scrollEnabled = false
            courseIntroTextView.text = content[0] as! String
            cell.addSubview(courseIntroTextView)
        } else {
            var lineImg = UIImageView(frame: CGRectMake(15, 0, UIScreen.mainScreen().bounds.size.width - 15, 1))
            lineImg.image = UIImage(named: "线.png")
            cell.addSubview(lineImg)
            
            var label = UILabel(frame: CGRectMake(10, 11, 200, 21))
            let font = UIFont.boldSystemFontOfSize(15)
            label.font = font
            label.textColor = UIColor.blackColor()
            label.text = "讲师简介"
            cell.addSubview(label)
            
            cell.addSubview(self.teacherIntroImgView)
            
            var teacherIntroTextView = UITextView(frame: CGRectMake(88, 32, UIScreen.mainScreen().bounds.size.width - 98, teacherIntroHeight))
            let font2 = UIFont.systemFontOfSize(14)
            teacherIntroTextView.font = font2
            teacherIntroTextView.textColor = UIColor.grayColor()
            teacherIntroTextView.editable = false
            teacherIntroTextView.scrollEnabled = false
            teacherIntroTextView.text = content[1] as! String
            cell.addSubview(teacherIntroTextView)
        }
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if (indexPath.row == 0) {
            return self.courseIntroHeight + 41
        } else {
            let a = 122 as CGFloat
            let b = self.teacherIntroHeight + 42
            if (a > b) {
                return a
            } else {
                return b
            }
        }
    }
    
    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        var view = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 20))
        view.backgroundColor = UIColor.whiteColor()
        return view
    }
    
    
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
